import os
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from matplotlib.font_manager import FontProperties
from matplotlib import gridspec

fs = 14
font = {'family' : 'times new roman',
        'weight' : 'normal',  
        'size'   : fs,  
        }

numHosts  = 10
numToRs   = 100
numCores  = 4
numOCSes  = 4
numDemands = 1
flowsNum_b = 1

scheduler   = ['unicast', 'binomial', 'ring', 'EPS', 'OCS', 'Jcast']
update_enable = True

x           = []
y_unicast   = []
y_binomial  = []
y_ring      = []
y_EPS       = []
y_OCS       = []
y_Jcast     = []

groupSizes  = [(11, 11)] # 10, 20, 30, 40, 50, 60, 70, 80, 90, 100
sampe = 20
flowSizes = []
for i in xrange(1,sampe+1): # 50MB ~ 1000MB
    size = i*50
    x.append(size)
    flowSizes.append((size, size))

iteration = 1 # run times
for i in xrange(1, iteration+1):
    for (groupSize_a, groupSize_b) in groupSizes: # different groupSize
        for (flowSize_a, flowSize_b) in flowSizes: # different flowSize
            for sched in scheduler:
                mflowsFileName = "%sCore_%sOCS_%sToR_%sHost_%sDemand_%sFlows_%s%sSize_%s%sGroup_%s_%s_%s.txt"\
                            %(numCores, numOCSes, numToRs, numHosts, numDemands, flowsNum_b, flowSize_a, flowSize_b, groupSize_a, groupSize_b, i, sched, update_enable)

                if os.path.isfile(mflowsFileName):
                    with open(mflowsFileName, 'r') as f:
                        for line in f.readlines():
                            split = line.split()
                            fct = float(split[5])
                            if sched == 'unicast':
                                y_unicast.append(fct)
                            elif sched == 'binomial':
                                y_binomial.append(fct)
                            elif sched == 'ring':
                                y_ring.append(fct)
                            elif  sched == 'EPS':
                                y_EPS.append(fct)
                            elif  sched == 'OCS':
                                y_OCS.append(fct)
                            elif sched == 'Jcast':
                                y_Jcast.append(fct)

print y_unicast
print y_Jcast

fig = plt.figure(figsize=(6,3.9))
marker_set = ['+', 's', 'o', 'x', '<', 'D']
markers = {}
for i in range(0, len(scheduler)):
    markers[scheduler[i]] =  marker_set[i]

lw = 1
ms = 6
plt.plot(x, y_Jcast, 'D', color= 'y', ls='-.', lw=lw, ms=ms, zorder=3)
plt.plot(x, y_OCS, '<', color= 'r', ls='-.', lw=lw, ms=ms+2)
plt.plot(x, y_EPS, 'x', color= 'blue', ls='-.', lw=lw, ms=ms+2,mew=2)
plt.plot(x, y_ring, 'o', color= 'purple', ls='-.', lw=lw, ms=ms)
plt.plot(x, y_binomial, 's',color= 'seagreen', ls='-.', lw=lw, ms=ms)
plt.plot(x, y_unicast, '+', color='black', ls='-.', lw=lw, ms=ms+2, mew=2)

plt.subplots_adjust(bottom=0.14, top=0.9, left=0.12, right=0.95)
plt.yscale('log')

plt.xlim(45, 1010)
plt.xlabel('Large Multicast Flow Size (MB)', fontdict=font)
plt.ylabel('Multicast Flow Completion Time (s)', fontdict=font)
plt.xticks((50, 200, 400, 600, 800, 1000), (50, 200, 400, 600, 800, 1000),fontproperties="times new roman", fontsize=fs)
plt.yticks(fontproperties='times new roman')
plt.text(65, 200, 'Group Size = 50', fontdict = font, backgroundcolor='w')
plt.legend( ('Jcast', 'OCS', 'EPS', 'Ring', 'Binomial Tree', 'Naive Unicast'), ncol=2, \
            prop={'family':'times new roman', 'size': 11}, loc=4) #loc=2, fontsize=fs
plt.grid(ls='--')
plt.savefig('0_Simulation_LargeFlowSize_FCT.pdf')
plt.show()
